package satir.card.factory;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;

import satir.card.Card;
import satir.card.EventCard;
import satir.card.Question;
import satir.framework.LazyInitiator;

/**
 * A factory for creating EventCard objects.
 * @version 3.0 April 2012
 * @author Creative Family
 */
public class EventCardFactory extends CardFactory {

    /**
     * Break down the structure of cardJson to retrieve required data And then
     * create Choice, Question, EventCard accordingly.
     * @return Instance of EventCard
     * @see LazyInitiator
     */
    @Override
    public Card createCard() {
	String key = "";
	String name = "";
	ArrayList<Question> questions = new ArrayList<Question>();
	JSONArray questionsKeys;
	try {
	    key = cardJson.getString("key");
	    name = cardJson.getString("name");
	    questionsKeys = cardJson.getJSONArray("questions");
	    for (int i = 0; i < questionsKeys.length(); i++) {
		String questionKey = questionsKeys.getString(i);
		Question question = (Question) LazyInitiator.get("Question",
			questionKey);
		questions.add(question);
	    }
	} catch (JSONException e) {
	    e.printStackTrace();
	}
	return new EventCard(key, name, questions);
    }
}
